var common = require('../servers/fight/common');
var utils = require('../util/utils');
var rooms = module.exports;
rooms.matchingRooms = [[],[],[]];//三个房间用于存放正在匹配的玩家
rooms.matchedRooms = [[],[],[]];//三个房间用于存放匹配了的玩家
rooms.addPlayer = function(player)
{
    console.info('add player:',player);
    rooms.matchingRooms[player.roomId].push(player);
}
rooms.removePlayer = function(player)
{
    console.info('remove player:',player);
    var room = rooms.matchingRooms[player.roomId];
    for( var i in room )
    {
        var item = room[i];
        if( player.uid == item.uid )
        {
            room.splice(i,1);
            return;
        }
    }
}

rooms.addTable = function(table)
{
    if( table )
    {
        table.time = utils.getTime();
        console.info('add table:',table);
        rooms.matchedRooms[table.uid1.roomId].push(table);
    }
}

rooms.removeTable = function(player,cb)
{
    var roomId = player.roomId;
    var uid = player.uid;
    var room = rooms.matchedRooms[roomId];
    for( var i in room )
    {
        var table = room[i];
        if( uid == table.uid1.uid || uid == table.uid2.uid )
        {
            room.splice(i,1);
            console.info("remove table:",table);
            cb(table);
            return;
        }
    }
    cb(0);
}

rooms.updateTable = function(player,cb)
{
    var roomId = player.roomId;
    var room = rooms.matchedRooms[roomId];
    for( var i in room )
    {
        var item = room[i];
        if( player.uid == item.uid1.uid || player.uid == item.uid2.uid )
        {
            (player.uid == item.uid1.uid)?(item.uid1 = player):(item.uid2 = player);
            cb(item);
            return;
        }
    }
    cb(0);
}